# -*- coding: utf_8 -*-
# @Create   : 2021/8/20 9:06
# @Author   : yh
# @Remark   : 视频管理
import json
import os
from typing import List

from mxsoftpy import View
from mxsoftpy.exception import DataError

from server.common.VideoManage.VideoQuality.video import VideoServer
from utils.conf.mxconfig import MxConfig
from utils.public.export_execl import ExportExecl
from utils.public.params_method import check_params, pro_decode


class VideoTest(View):
    def post(self):
        """
        测试视频是否可用
        """
        check_params(self.request.POST, ['password', 'pwd_verify'])
        if VideoServer().test(**pro_decode(**self.request.POST)):
            return '测试成功', ''
        return '测试失败'


class VideoAdd(View):

    def post(self):
        """
        添加视频
        """
        check_params(self.request.POST, ['password', 'pwd_verify'])
        param = pro_decode(**self.request.POST)
        return '新增成功', {'video_code': VideoServer().create(**param)}


class VideoDelete(View):

    def post(self):
        """
        删除视频，支持单个code或者批量删除
        """
        res_list = list()

        try:
            params_list = json.loads(self.request.POST['params'])
        except DataError:
            return 'false',DataError('数据格式错误')

        for params in params_list:
            check_params(params, ['video_code', 'delete_type'])
            res_list.extend(VideoServer().delete(**params))
        return '删除视频成功', {'video_code': res_list}


class VideoDeleteCheck(View):

    def post(self):
        """
        删除视频判断，支持单个code或者批量删除
        """
        check_params(self.request.POST, ['video_code'])
        return '删除视频判断成功', {'video_code': VideoServer().delete_check(**self.request.POST)}


class VideoRetrieve(View):

    def post(self):
        """
        视频详情(编辑回显)
        """
        check_params(self.request.POST, ['video_code'])
        return '获取视频详情成功', VideoServer().retrieve(**self.request.POST)


class VideoUpdate(View):

    def post(self):
        """
        视频更新
        """
        check_params(self.request.POST, ['video_code'])
        param = pro_decode(**self.request.POST)
        return '更新视频成功', VideoServer().update(**param)


class VideoMove(View):

    def post(self):
        """
        视频移动
        """
        check_params(self.request.POST, ['video_code', 'group_code'])
        return '移动视频成功', VideoServer().move(**self.request.POST)


class VideoSelect(View):

    def post(self):
        """
        根据设备名称查询视频
        """
        check_params(self.request.POST, ['name'])
        param = pro_decode(**self.request.POST)
        return '查询视频成功', VideoServer().select(**param)


class VideoPreview(View):

    def post(self):
        """
        展示视频或图像
        """
        check_params(self.request.POST, ['video_code', 'task_code', 'pre_code'])
        return '视频预览成功', VideoServer().preview(**self.request.POST)


# 视频批量导入上传及下载的模版
_content = {
    "header": "批量导入视频 码流(1主、2副) 设备类型(1摄像头、2录像机、3其他) 数据状态(1启用、0禁用)",
    "headline": {
        "group_name": "视频组名称",
        "name": "视频名称",
        "server_ip": "服务器IP",
        "device_ip": "设备IP",
        "url": "URL",
        "port": "端口",
        "username": "用户名",
        "password": "密码",
        "stream_code": "码流",
        "aisle": "通道",
        "device_type_code": "设备类型",
        "vendor": "供应商名称",
        "data_state": "数据状态"
    },
    "select": {"stream_code": ['1', '2'], "device_type_code": ["1", "2", "3"], "data_state": ["0", "1"]}
}


class VideoImportTemplate(View):

    def post(self):
        """
        视频导入模版下载
        """
        ExportExecl().template_download(_content)(MxConfig.TMP_DIR + 'video_import.xlsx')
        return '模版下载成功', {'execl_path': os.sep + "tmp" + os.sep + 'video_import.xlsx'}


class VideoImport(View):
    def post(self):
        """
        从excel导入视频
        """
        excel_name = self.request.upload(MxConfig.TMP_DIR)
        try:
            excel_data: List[dict] = ExportExecl.read_execl(MxConfig.TMP_DIR + excel_name, _content)  # 读取上传的excel
        except:
            return '模板导入失败'
        success, error = VideoServer().excel_import(excel_data)
        if error:
            return '导入失败%s条' % error
        return "导入成功%s条" % success, ''

